﻿/*
 * Copyright © 2020, Wuxi Safover Intelligent Technology Co., Ltd. All rights reserved.
 * 
 * 无锡安永智能科技有限公司版权所有，未经许可，不得用于商业用途
 * 
 * Visit: https://www.safover.com/ 
 */

using log4net;
using System;
using System.Reflection;
using Safover.LidarCollector.Models;
using static Safover.LidarCollector.Managers.DeviceManager;

namespace Safover.LidarCollector.Managers
{
    /// <summary>
    /// Only for control request originiated from UI
    /// Do not use in work scenarios.
    /// </summary>
    public class UiDeviceController
    {
        #region Singleton
        public static UiDeviceController Instance { get; } = new UiDeviceController();
       

        private UiDeviceController() { }

        #endregion

        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private const string SUCCESS = "Success";

        public void OpenRotaryTable()
        {
            DeviceFactoryInstance.InitializeRotaryTable();
        }

        public void CloseRotaryTable()
        {
            DeviceFactoryInstance.CloseRotaryTable();
        }


        public void OpenOpticalMaser()
        {
            DeviceFactoryInstance.InitializeOpticalMaser();
        }

        public void CloseOpticalMaser()
        {
            DeviceFactoryInstance.CloseOpticalMaser();
        }

        public void OpenGpsLocator()
        {
            DeviceFactoryInstance.InitializeGpsLocator();
        }

        public void CloseGpsLocator()
        {
            DeviceFactoryInstance.CloseGpsLocator();
        }

        public void OpenElectronicCompass()
        {
            DeviceFactoryInstance.InitializeElectronicCompass();
        }

        public void CloseElectronicCompass()
        {
            DeviceFactoryInstance.CloseElectronicCompass();
        }


        public void OpenAcquisitionCard()
        {
            DeviceFactoryInstance.InitializeAcquisitionCard();
        }

        public void CloseAcquisitionCard()
        {
            DeviceFactoryInstance.CloseAcquisitionCard();
        }

        public string StartLasar()
        {
            try
            {
                _ = DeviceFactoryInstance.OpticalMaserDevice.StartLasar();
            }
            catch (Exception ex)
            {
                Log.Info(ex.Message, ex);
                return ex.Message;
            }

            return SUCCESS;
        }

        public string StopLasar()
        {
            try
            {
                _ = DeviceFactoryInstance.OpticalMaserDevice.StopLasar();
            }
            catch (Exception ex)
            {
                Log.Info(ex.Message, ex);
                return ex.Message;
            }

            return SUCCESS;
        }

        public string ErrorResetLaser()
        {
            try
            {
                DeviceFactoryInstance.OpticalMaserDevice.ErrorReset();
            }
            catch (Exception ex)
            {
                Log.Info(ex.Message, ex);
                return ex.Message;
            }

            return SUCCESS;
        }

        public string MoveHorizontallyTo(float angle)
        {
            try
            {

                DeviceFactoryInstance.RotaryTableDevice.MoveHorizontallyTo(angle);
            }
            catch (Exception ex)
            {
                Log.Info(ex.Message, ex);
                return ex.Message;
            }

            return SUCCESS;
        }

        public string MoveVerticallyTo(float angle)
        {
            try
            {
                DeviceFactoryInstance.RotaryTableDevice.MoveVerticallyTo(angle);

            }
            catch (Exception ex)
            {
                Log.Info(ex.Message, ex);
                return ex.Message;
            }

            return SUCCESS;
        }

        public string QueryHorizontalAngle()
        {
            try
            {

                DeviceFactoryInstance.RotaryTableDevice.QueryHorizontalAngle();
            }
            catch (Exception ex)
            {
                Log.Info(ex.Message, ex);
                return ex.Message;
            }

            return SUCCESS;
        }

        public string QueryVerticalAngle()
        {
            try
            {
                DeviceFactoryInstance.RotaryTableDevice.QueryVerticalAngle();

            }
            catch (Exception ex)
            {
                Log.Info(ex.Message, ex);
                return ex.Message;
            }

            return SUCCESS;
        }

        public string InitAcquisitionCardParams(int samplingDepth, int cumulativeGrequency)
        {
            try
            {
                _ = DeviceFactoryInstance.AcquisitionCard.InitParams(samplingDepth, cumulativeGrequency);
            }
            catch (Exception ex)
            {
                Log.Info(ex.Message, ex);
                return ex.Message;
            }

            return SUCCESS;
        }

        public string ResetAcquisitionCard()
        {
            try
            {
                _ = DeviceFactoryInstance.AcquisitionCard.Reset();
            }
            catch (Exception ex)
            {
                Log.Info(ex.Message, ex);
                return ex.Message;
            }

            return SUCCESS;
        }

        public string StopMovingRotaryTable()
        {
            try
            {
                DeviceFactoryInstance.RotaryTableDevice.StopMove();
            }
            catch (Exception ex)
            {
                Log.Info(ex.Message, ex);
            }

            return SUCCESS;
        }

        public ElectronicCompassAngles? QueryAngles()
        {
            try
            {
                return DeviceFactoryInstance.ElectronicCompass.QueryAngles();
            }
            catch (Exception ex)
            {
                Log.Info(ex.Message, ex);
            }

            return null;
        }


        public GpsInformation QueryGpsInformation()
        {
            try
            {
                return DeviceFactoryInstance.GpsLocatorDevice.ObtainGpsInformation();
            }
            catch (Exception ex)
            {
                Log.Info(ex.Message, ex);
            }

            return null;
        }
    }
}
